/**
 * Filename:Ajax.js
 * Used as a ajax engine.
 * 
 * @author Nonsenser
 * mail:n0nsenser@hotmail.com
 */
if(!this.Ajax)var Ajax = function()
{
	this.VERSION = '0.01';
	this.hreq = false;
	if(window.XMLHttpRequest) 
	{
		this.hreq = new XMLHttpRequest();
	}
	else if(window.ActiveXObject)
	{
		try{
			this.hreq = new ActiveXObject("Microsoft.XMLHTTP");
		}
		catch(e)
		{
			this.hreq = new ActiveXObject("Msxml2.XMLHTTP");
		}
	}
	if(this.hreq == null)
	{
		throw new Error("XMLHttpRequest not supported");
	}
	if(this.hreq.readyState != 0)
	{
		this.die("Ajax readyState should be 0!");
	}
}

Ajax.prototype.get = function(url,callback,is_asyn)
{
	var hreq = this.hreq;
	
	hreq.open('GET',url,is_asyn);
	hreq.send(null);
	if(is_asyn)
	{
		hreq.onreadystatechange = function()
		{
			if(hreq.readyState == 4)
			{
				if(hreq.status == 200)
				{
					callback(hreq.responseText);
				}else
				{ alert("The page you requested is unnormal."); }
			}
		}
	}
	if(!is_asyn)
	{
		callback(hreq.responseText);
	}
}

Ajax.prototype.post = function(url,data,callback,is_asyn)
{
	var hreq = this.hreq;
	if(is_asyn)
	{
		hreq.onreadystatechange = function()
		{
			if(hreq.readyState == 4)
			{
				if(hreq.status == 200)
				{
					callback(hreq.responseText);
				}else
				{ alert("The page you requested is unnormal."); }
			}
		}
	}
	hreq.open('POST',url,is_asyn);
	hreq.setRequestHeader("Content-Type","application/x-www-form-urlencoded");//it must be after calling the open method
	hreq.send(data);
	if(!is_asyn)
	{
		callback(hreq.responseText);
	}
}

Ajax.prototype.die = function(param)
{
	throw(param);
}

Ajax.prototype.toString = function()
{
	return "A ajax demo";
}